﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MetroFramework.Forms;
using Managistics.Warehouse.Views.Interfaces;
using Managistics.Warehouse.Views.Purchases;

namespace Managistics.Warehouse
{
    public partial class MainForm : MetroForm, IMainView
    {
        private int childFormNumber = 0;

        public MainForm()
        {
            InitializeComponent();

            base.Closed += delegate { Closed(); };
        }

        # region Action Parameters

        public event Action ShowSubFormClicked = delegate { };
        public event Action ShowDashoardFormClicked = delegate { };

        // Purchase
        public event Action ShowViewEnterFromPurchaseFormClicked = delegate { };
        public event Action ShowPurchaseReportsFormClicked = delegate { };
        public event Action ShowViewEnterInputSheetFormClicked = delegate { };

        // Inventory
        public event Action ShowAdjustParityRetailCodeFormClicked = delegate { };
        public event Action ShowCalculateTotalInOutputFormClicked = delegate { };

        // Sale
        public event Action ShowSaleReportsFormClicked = delegate { };
        public event Action ShowViewEnterOutputFromSaleFormClicked = delegate { };
        public event Action ShowViewEnterOutputSheetFormClicked = delegate { };

        public new event Action Closed = delegate { };

        # endregion

        # region All Events

        # endregion

        # region Funcs on interface
        public void Open()
        {
            // MainForm is special because it blocks until closed...
            Application.Run(this);
        }
        public void Open(IView mainView)
        {
        }
        # endregion


        # region Control actions on menu items

        /// <summary>
        /// control display forms
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void ShowNewForm(object sender, EventArgs e)
        {
            Form childForm = new Form();
            childForm.MdiParent = this;
            childForm.Text = "Window " + childFormNumber++;
            childForm.Show();
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = openFileDialog.FileName;
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void viewEnterFromSalesOrderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowViewEnterOutputFromSaleFormClicked();
        }

        private void importToCreateRelationToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void testSubformToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowSubFormClicked();
        }

        private void dashboardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // only allow open least one child form type
            if (!CheckIsOpenedMdiChildForm(new Dashboard()))
            {
                ShowDashoardFormClicked();
            }
        }

        # endregion

        private bool CheckIsOpenedMdiChildForm(Form newForm)
        {
            bool found = false;
            if (this.MdiChildren.Length > 0)
            {
                foreach (Form frm in this.MdiChildren)
                {
                    if (frm.GetType() == newForm.GetType())
                    {
                        frm.WindowState = FormWindowState.Maximized;
                        this.Dock = DockStyle.Fill;
                        frm.Show();
                        frm.BringToFront();
                        found = true;
                        break;
                    }
                }
            }
            return found;
        }

        /// <summary>
        /// Show the view / enter form the Purchase Order
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void viewEnterFromPurchasesOrderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // only allow open least one child form type
            if (!CheckIsOpenedMdiChildForm(new ViewEnterInputFromPurchase()))
            {
                ShowViewEnterFromPurchaseFormClicked();
            }
        }

        private void viewEnterInputSheetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowViewEnterInputSheetFormClicked();
        }

        private void menuPurchaseReports_Click(object sender, EventArgs e)
        {
            ShowPurchaseReportsFormClicked();
        }

        private void menuAdjustParityRetailCodeStock_Click(object sender, EventArgs e)
        {
            ShowAdjustParityRetailCodeFormClicked();
        }

        private void calculateInputOutputInMonthToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowCalculateTotalInOutputFormClicked();
        }

        private void menuSaleReport_Click(object sender, EventArgs e)
        {
            ShowSaleReportsFormClicked();
        }

        private void menuViewEnterOutputSheet_Click(object sender, EventArgs e)
        {
            ShowViewEnterOutputSheetFormClicked();
        }
    }
}
